package com.share.core.jdbc;

import com.share.core.service.ExampleService;
import com.share.core.type.ShardingType;
import com.share.core.util.ExampleExecuteTemplate;
import com.share.core.jdbc.factory.RangeDataSourceFactory;
import com.share.core.jdbc.repository.AddressRepositoryImpl;
import com.share.core.jdbc.repository.OrderItemRepositoryImpl;
import com.share.core.jdbc.repository.RangeOrderRepositoryImpl;
import com.share.core.jdbc.service.OrderServiceImpl;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author caifan
 * @created 2021/8/13
 * description:
 */
public class JavaRangeConfigurationMain {
    private static ShardingType shardingType = ShardingType.SHARDING_DATABASES;
//    private static ShardingType shardingType = ShardingType.SHARDING_TABLES;
//    private static ShardingType shardingType = ShardingType.SHARDING_DATABASES_AND_TABLES;
//    private static ShardingType shardingType = ShardingType.READWRITE_SPLITTING;
//    private static ShardingType shardingType = ShardingType.SHARDING_READWRITE_SPLITTING;

    public static void main(final String[] args) throws SQLException {
        DataSource dataSource = RangeDataSourceFactory.newInstance(shardingType);
        ExampleExecuteTemplate.run(getExampleService(dataSource));
    }

    private static ExampleService getExampleService(final DataSource dataSource) {
        return new OrderServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource), new AddressRepositoryImpl(dataSource));
    }
}
